package com.ado.bazinga.core.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ado.bazinga.core.annotation.AdminLoginToken;
import com.ado.bazinga.core.annotation.ApiDesc;
import com.ado.bazinga.core.common.http.HttpResult;
import com.ado.bazinga.core.common.params.vo.DataStatsVO;
import com.ado.bazinga.core.dal.entity.Good;
import com.ado.bazinga.core.service.GoodService;
import com.ado.bazinga.core.service.OrderService;

/**
 *@author adonais
 *@date 2020年4月7日
 *@description DataController.java
**/
@RestController
@RequestMapping(value = "/api/data")
public class DataController {
	
	@Autowired
	@Qualifier(value = "orderService")
	private OrderService orderService ;
	@Autowired
	@Qualifier(value = "goodService")
	private GoodService goodService ;
	
	@ApiDesc(desc = "数据统计接口")
	@AdminLoginToken
	@GetMapping(value = "/stats")
	public HttpResult stats() {
		DataStatsVO dataStatsVO = new DataStatsVO() ;
		Map<String,String> statsMap = new HashMap<String, String>() ;
		dataStatsVO.setOrderNum(String.valueOf(orderService.getAllOrders().size()));
		dataStatsVO.setSaleAmount(String.valueOf(goodService.countSoldNum())) ;
		List<Good> goods = goodService.getAllGoods() ;
		BigDecimal total = new BigDecimal(0) ;
		for (Good good : goods) {
			total = total.add(good.getPresentPrice().multiply(new BigDecimal(good.getSold()))) ;
		}
		dataStatsVO.setSaleAmount(total.toString());
		dataStatsVO.setStockNum(String.valueOf(goodService.countStockNum()));
		statsMap.put("销售数量" , dataStatsVO.getSaleNum()) ;
		statsMap.put("销售总金额", dataStatsVO.getSaleAmount()) ;
		statsMap.put("商品剩余库存总数" , dataStatsVO.getStockNum()) ;
		statsMap.put("订单总数", dataStatsVO.getOrderNum()) ;
		return HttpResult.ok(statsMap) ;
	}

}
